﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using EPiServer.Security;
using EPiServer.Web.Hosting;

namespace Hosting
{
    public class FlickrDirectory : UnifiedDirectory
    {
        private readonly string _photoSetId;

        public FlickrDirectory(VirtualPathUnifiedProvider provider, string virtualPath, string photoSetId)
            : base(provider, virtualPath, null, true)
        {
            _photoSetId = photoSetId;
        }

        internal bool ContainsDirectory(string name)
        {
            return (GetDirectory(name) != null);
        }

        internal bool ContainsFile(string name)
        {
            return (GetFile(name) != null);
        }

        public override UnifiedFile CreateFile(string name)
        {
            return CreateFile(name, Guid.NewGuid());
        }

        public override UnifiedFile CreateFile(string name, Guid id)
        {
            var virtualPath = Provider.CombineVirtualPaths(VirtualPath, name);
            return new FlickrFile(this, Provider, virtualPath, true, null);
        }

        public override void CopyTo(string newVirtualPath)
        {
        }

        public override AccessControlList ACL
        {
            get
            {
                var acl = new AccessControlList();
                if (BypassAccessCheck)
                {
                    acl.Add(new AccessControlEntry("Everyone", AccessLevel.FullAccess, SecurityEntityType.Role));
                }
                return acl;
            }
        }

        public override void Delete()
        {
        }

        public override UnifiedDirectory[] GetDirectories()
        {
            return Directories
                .Cast<FlickrDirectory>()
                .ToArray();
        }

        internal FlickrDirectory GetDirectory(string name)
        {
            var dir = Directories;
            foreach (FlickrDirectory directory in dir)
            {
                if (string.Compare(directory.Name, name, true) == 0)
                {
                    return directory;
                }
            }

            return null;
        }

        internal FlickrFile GetFile(string name)
        {
            foreach (FlickrFile file in Files)
            {
                if (string.Compare(file.Name, name, true) == 0)
                {
                    return file;
                }
            }
            return null;
        }
       
        public override UnifiedFile[] GetFiles()
        {
            return Files
                .Cast<FlickrFile>()
                .ToArray();
        }

        public override IEnumerable Directories
        {
            get
            {
                var directories = new List<FlickrDirectory>();
                if (IsFirstLevel)
                {
                    var photoSets = FlickrDataFactory.Instance.GetPhotoSets(((FlickrVirtualPathProvider)Provider).ServiceUrl);
                    foreach (var set in photoSets)
                    {
                        var virtualPath = Provider.CombineVirtualPaths(VirtualPath, set.Title);
                        var item = new FlickrDirectory(Provider, virtualPath, set.ID);
                        directories.Add(item);
                    }
                }

                return directories;
            }
        }

        public override IEnumerable Files
        {
            get
            {
                var files = new List<FlickrFile>();
                if(!string.IsNullOrEmpty(_photoSetId))
                {
                    // get all photos in this photoset (directory) from Flickr
                    // and create an array of FlickrFile objects
                    var photos = FlickrDataFactory.Instance.GetPhotos(((FlickrVirtualPathProvider)Provider).ServiceUrl, _photoSetId);
                    foreach (var photo in photos)
                    {
                        var virtualPath = Provider.CombineVirtualPaths(VirtualPath, photo.Title);
                        var file = new FlickrFile(this, Provider, virtualPath,false, photo);
                        files.Add(file);
                    }
                }

                return files;
            }
        }

        public override IEnumerable Children
        {
            get
            {
               var list = Directories.Cast<object>().ToList();
                list.AddRange(Files.Cast<object>());
                return list;
            }
        }

        public override bool IsFirstLevel
        {
            get
            {
                return (Parent == null);
            }
        }

        public override UnifiedDirectory Parent
        {
            get
            {
                return (HostingEnvironment.VirtualPathProvider.GetDirectory(VirtualPathUtility.GetDirectory(VirtualPath)) as FlickrDirectory);
            }
        }
    }
}
